#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Neatly.UI
{
    public class NeatlyUI
    {
        public delegate bool DelRtl();
        public delegate string DelI18nLabel(string key);
        //RTL Action
        public static DelRtl IsRtlAction { get; private set; }
        //下载UrlSprite
        public static Action<NImage, string> DownloadUrlSpriteAction { get; private set; }
        //Emoji目录集
        public static Dictionary<string, string> EmojiRects { get; private set; }
        //Sprite白边信息
        public static Dictionary<string, Vector4> PaddingConfig { get; private set; }
        //设置Sprite函数
        public static Action<ImageInterface, string> SetSpriteAction { get; private set; }
        //增加图集引用函数
        public static Action<ImageInterface> AddAtlasRefAction { get; private set; }
        //解除图集引用函数
        public static Action<ImageInterface> LoseAtlasRefAction { get; private set; }
        //播放音效函数
        public static Action<string> PlaySoundAction { get; private set; }
        //按钮点击Scale
        private static float m_ButtonScaleSize = 1.1f;
        public static float ButtonScaleSize => m_ButtonScaleSize;
        //按钮Tween时间
        private static float m_ButtonTweenTime = 0.1f;
        public static float ButtonTweenTime => m_ButtonTweenTime;
        //按钮禁用色
        public static Color ButtonDisabledColor {get;} = new(0, 0, 0, 255);
        //文本多语言
        public static DelI18nLabel I18NLabel { get; private set; }

        //注册Rtl Action
        public static void RegisterIsRtlAction(DelRtl action)
        {
            IsRtlAction = action;
        }
        
        //注册UrlSprite下载
        public static void RegisterDownloadUrlSpriteAction(Action<NImage, string> action)
        {
            DownloadUrlSpriteAction = action;
        }
        
        //注册emoji目录集
        public static void RegisterEmojiDirectory(string[] emojiDirectory)
        {
            EmojiRects = new Dictionary<string, string>();
            for (int i = 0; i < emojiDirectory.Length; i++)
            {
                var lib = emojiDirectory[i];
                EmojiRects[UIHelper.GetConvertedString(lib)] = lib;
            }
        }

        //注册padding信息
        public static void RegisterPaddingConfig(Dictionary<string, Vector4> config)
        {
            PaddingConfig = config;
        }

        //注册图片设置函数
        public static void RegisterSetSpriteAction(Action<ImageInterface, string> action)
        {
            SetSpriteAction = action;
        }

        //注册增加图集引用
        public static void RegisterAddAtlasRef(Action<ImageInterface> action)
        {
            AddAtlasRefAction = action;
        }

        //注册解除图集引用
        public static void RegisterLoseAtlasRef(Action<ImageInterface> action)
        {
            LoseAtlasRefAction = action;
        }

        //注册ButtonScaleTween值
        public static void RegisterButtonScaleTween(float scaleSize, float tweenTime)
        {
            m_ButtonScaleSize = scaleSize;
            m_ButtonTweenTime = tweenTime;
        }

        //注册获取多语言文本
        public static void RegisterGetI18nLabelAction(DelI18nLabel action)
        {
            I18NLabel = action;
        }

        //注册播放音效函数
        public static void RegisterPlaySoundAction(Action<string> action)
        {
            PlaySoundAction = action;
        }
#if UNITY_EDITOR
        public static Dictionary<string, Vector4> GetEditorPadding()
        {
            string path = "Assets/data/ui/atlasconfig.asset";
            AtlasConfig atlasConfig = UnityEditor.AssetDatabase.LoadAssetAtPath<AtlasConfig>(path);
            var spritePaddings = new Dictionary<string, Vector4>();
            foreach (var atlas in atlasConfig.atlasList)
            {
                foreach (var spriteInfo in atlas.paddingList)
                {
                    if (spritePaddings.ContainsKey(spriteInfo.name)) continue;
                    spritePaddings.Add(spriteInfo.name, spriteInfo.padding);
                }
            }
            return spritePaddings;
        }
#endif
    }
}
